// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var base = require("base");
	var databind = require("databind");
	var tools = require("tools");
	var type;
	var datas1;
	var datas2;
	var datas3;
	var datas4;
	var typeNum;
	var text = "收付款动态";
	var tpl = $(".dataTpl1").html();

	var init = function() {
		type = base.getUrlParam("type");
		if(type == 1) {
			typeNum = 2;
			$("h3").html("当前贷款发放");
			tpl = $(".dataTpl1").html();
		} else if(type == 2) {
			typeNum = 1;
			$("h3").html("累计贷款发放");
			tpl = $(".dataTpl1").html();
		} else if(type == 3) {
			typeNum = 1;
			$("h3").html("累计还款");
			tpl = $(".dataTpl2").html();
		} else if (type == 4) {
			typeNum = 1;
			$("h3").html("贷款利息收取报表");
			tpl = $(".dataTpl3").html();
		} else if (type == 5) {
			typeNum = 2;
			$("h3").html("当前还款");
			tpl = $(".dataTpl2").html();
		} else if (type == 6) {
			typeNum = 3;
			$("h3").html("当日还款");
			text = "收付款动态(近七日)"
			tpl = $(".dataTpl2").html();
		} else if (type == 7) {
			typeNum = 3;
			$("h3").html("当日放款");
			text = "收付款动态(近七日)";
			tpl = $(".dataTpl1").html();
		}
		inqurieReceivePaymentDetail(typeNum);
		inqurieReceivePaymentList(typeNum);
		initEvent();
	};
	var initEvent = function() {};
	
	var inqurieReceivePaymentList = function(typeNum) {
		base.fnAjax({
			action: "inqurieReceivePaymentList.json",
			async: true,
			nocode: true,
			oData: {
				type: typeNum
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					juicer.register("formatCurrency", tools.formatCurrency);
					var html = juicer(tpl, data);
					$(".dataBody").html(html);
					
					var k=0;
					$('.con3 li.last').on('click',function(){
						if(k==0){
							$(this).siblings('.hidden').slideDown(500);
							$(this).children('img').attr('src','img/down.png');
							k=1;
						}else if(k==1){
							$(this).siblings('.hidden').slideUp(500);
							$(this).children('img').attr('src','img/up.png');
							k=0;
						}
					})
				} else {
					layer.msg(data.message);
				}
			}
		});
	};

	var inqurieReceivePaymentDetail = function(typeNum) {
		base.fnAjax({
			action: "inqurieReceivePaymentDetail.json",
			async: true,
			nocode: true,
			oData: {
				type: typeNum
			}, //传递参数
			fnSuccess: function(data) {
				if(data.status == 200) {
					datas1 = data.datas[0].dates;
					datas2 = data.datas[0].receives;
					datas3 = data.datas[0].payments;
					datas4 = data.datas[0].netInflows;
					showChart();
				} else {
					layer.msg(data.message);
				}
			}
		});
	};

	var showChart = function() {
		var chart = echarts.init(document.getElementById("chart"));
		var myChartContainer = function () {
		    chart.style.width = window.innerWidth+'px';
		    chart.style.height = window.innerHeight+'px';
		};
		var option = {
			title: {
				text: '收付款动态（近七日）',
				x: 'center',
				left: 'center',
				textStyle: {
					color: "#566bfe",
					fontWeight: 'normal',
				},
				top:'5%'
			},
			legend: {
				orient: 'horizontal',
				x: 'center',
				bottom:0,
				data: ['收款', '放款', '净流入'],
				textStyle:{
					color:'#fff'
				}
			},
			color: ['#e43b3b','#29347b','#fcd729'],
			tooltip: {
				trigger: 'axis',
			},
			grid: {
				left: '3%',
				right: '10%',
				bottom: '10%',
				containLabel: true
			},
			xAxis: [{
				type: 'category',
				boundaryGap: false,
				data: datas1,
				axisTick: {
					alignWithLabel: true
				}
			}],
			yAxis: [{
				type: 'value'
			}],
			textStyle: {
				color: "#fff"
			},
			series: [{
				name: '收款',
				type: 'bar',
				barWidth:'25%',
				data: datas2
			},{
				name: '放款',
				type: 'bar',
				barWidth:'25%',
				data: datas3
			},{
				name: '净流入',
				type: 'line',
				stack: '总量',
				label: {
					normal: {
						show: true,
						position: 'top'
					}
				},
				areaStyle: {
					normal: {}
				},
				data: datas4
			}]
		};
		chart.setOption(option, true);
		 window.onresize = function () {
		    myChartContainer();
		    chart.resize();
		};
	};

	return {
		init: init
	};
});